// ==============================================================================
//
// This file is part of the WelHair
//
// Create by Welfony <support@welfony.com>
// Copyright (c) 2013-2014 welfony.com
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// ==============================================================================

#import "DropDownView.h"
#import "JMHeader.h"
#import "UIViewExt.h"


@interface DropDownView ()<UITableViewDataSource, UITableViewDelegate>
{
    NSArray *dataArray;
    NSInteger currIndex;
}

@property (nonatomic, strong) UIView *pointToView; // the start position
@property (nonatomic, strong) UIView *backGroundView;
@property (nonatomic, strong) UITableView *tableView;


@end
@implementation DropDownView

- (id)initWithFrame:(CGRect)frame
      contentHeight:(float) contentHeight{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT)];
        bgView.backgroundColor = [UIColor blackColor];
        bgView.alpha = 0.5;
        [bgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgTapped:)]];
        [self addSubview:bgView];
        
        self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frameWidth, contentHeight)];
        self.backGroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.backGroundView];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.backGroundView.frameWidth,contentHeight - 20)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.backGroundView addSubview:self.tableView];
            
        self.backGroundView.transform = CGAffineTransformMakeTranslation(0,-contentHeight);
        self.alpha = 0;
    }
    return self;
}


- (void)bgTapped:(UITapGestureRecognizer *)tap
{
    [self hide];
    if (_dismiss) {
        self.dismiss(self);
    }
}

- (void)showData:(NSArray *)datasource atIndex:(NSInteger)index pointToView:(UIView *)view SelectedItem:(SelectedItem)item diss:(DisMiss)diss{
    
    self.block = item;
    self.dismiss = diss;
    
    if(currIndex != index){
        [UIView transitionWithView:self.backGroundView duration:0.35 options:UIViewAnimationOptionCurveLinear animations:^{
            self.backGroundView.transform = CGAffineTransformMakeTranslation(0,-self.backGroundView.frameHeight - 64 - 40);
            self.alpha =0;
            currIndex = index;
        } completion:^(BOOL finished) {
            dataArray = datasource;
            [self.tableView reloadData];
            [UIView transitionWithView:self.backGroundView
                              duration:0.35
                               options:UIViewAnimationOptionCurveLinear
                            animations:^{
                                self.alpha = 1;
                                self.backGroundView.transform = CGAffineTransformMakeTranslation(0,0);
                            }
                            completion:NULL];
            self.pointToView = view;
        }];
    }else{
        if(self.pointToView != view){
            dataArray = datasource;
            [self.tableView reloadData];
            [UIView transitionWithView:self.backGroundView
                              duration:0.35
                               options:UIViewAnimationOptionCurveLinear
                            animations:^{
                                self.alpha = 1;
                                self.backGroundView.transform = CGAffineTransformMakeTranslation(0,0);
                            }
                            completion:NULL];
            self.pointToView = view;
        }

    }
}

- (void)hide
{
    
    [UIView transitionWithView:self.backGroundView duration:0.35 options:UIViewAnimationOptionCurveLinear animations:^{
        self.backGroundView.transform = CGAffineTransformMakeTranslation(0,-self.backGroundView.frameHeight);
    } completion:^(BOOL finished) {
        
        self.alpha = 0;
    }];
    self.pointToView = nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"DropDownCellIdentifier_1";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.textLabel.text = dataArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:12.0];
    cell.textLabel.textColor = UIColorFromRGB(0x989898);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self hide];
    self.block(dataArray[indexPath.row],self);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

@end
